<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }
        .register-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 350px;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .error-message {
            color: red;
            font-size: 14px;
            margin-top: 5px;
        }
        .success-message {
            color: green;
            font-size: 14px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
<div class="register-container">
    <h2>用户注册</h2>
    <form id="registerForm">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group">
            <label for="phone">手机号</label>
            <input type="tel" id="phone" name="phone">
        </div>
        <div class="form-group">
            <label for="email">邮箱</label>
            <input type="email" id="email" name="email">
        </div>
        <div class="form-group">
            <button type="button" onclick="register()">注册</button>
        </div>
        <div id="message" class="error-message"></div>
    </form>
</div>

<script>
    function register() {
        const form = document.getElementById('registerForm');
        const messageDiv = document.getElementById('message');

        const user = {
            userName: form.username.value,
            password: form.password.value,
            phone: form.phone.value,
            email: form.email.value
        };

        fetch('/user/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(user)
        })
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    messageDiv.textContent = data.msg || '注册成功！';
                    messageDiv.className = 'success-message';
                    form.reset();
                    setTimeout(() => {
                        window.location.href = '/login.html'; // 或你的首页路径
                    }, 3000);

                } else {
                    messageDiv.textContent = data.msg || '注册失败';
                    messageDiv.className = 'error-message';
                }
            })
            .catch(error => {
                messageDiv.textContent = '请求失败: ' + error.message;
                messageDiv.className = 'error-message';
            });
    }
</script>
</body>
</html>